package com.situ.mall.service.Impl;

import com.situ.mall.mapper.FootprintMapper;
import com.situ.mall.pojo.entity.Footprint;
import com.situ.mall.pojo.vo.FootprintVO;
import com.situ.mall.service.IFootprintService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FootprintServiceImpl implements IFootprintService {
    @Autowired
    private FootprintMapper footprintMapper;

    @Override
    public void addFootprint(Footprint footprint) {
        Integer footprintCount = footprintMapper.selectByProductId(footprint);
        if (footprintCount == 0) {
            // 添加新的足迹
            footprintMapper.insertSelective(footprint);
        } else {
            // 首先删除用户当天的足迹
            footprintMapper.deleteTodayFootprints(footprint);
            // 添加新的足迹
            footprintMapper.insertSelective(footprint);
        }
    }

    @Override
    public List<FootprintVO> listFootprint(Integer userId) {
        return footprintMapper.listFootprint(userId);
    }

    @Override
    public void delete(Footprint footprint) {
        footprintMapper.updateByPrimaryKeySelective(footprint);
    }
}
